package com.manbas.zhg.manbas.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.manbas.zhg.R;
import com.manbas.zhg.manbas.api.RegisterApi;
import com.manbas.zhg.manbas.global.BaseActivity;
import com.manbas.zhg.manbas.utils.HttpUtis;
import com.manbas.zhg.manbas.utils.LogUtil;
import com.manbas.zhg.manbas.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/11/8.
 * 注册页面
 */

public class RegisterActivity extends BaseActivity {
    public static final int SEND_VERIGY=0;

    @BindView(R.id.edt_phoneNum)
    EditText edt_phoneNum;
    @BindView(R.id.edt_password)
    EditText edt_password;
    @BindView(R.id.edt_verifyNum)
    EditText edt_verifyNum;
    @BindView(R.id.tv_getVerify)
    TextView tv_getVerify;
    @BindView(R.id.tv_register)
    TextView tv_register;

    private RegisterApi registerApi;
    private String phoneNum;
    private String password;
    private String verityNum;

    private Handler handler;
    private TimeCount time;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_activity);
        ButterKnife.bind(this);
        edt_phoneNum.setInputType(InputType.TYPE_CLASS_NUMBER);
        edt_phoneNum.setFilters(new InputFilter[]{new InputFilter.LengthFilter(11)});//限制输入长度
        edt_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
        time=new TimeCount(60000,1000);
        registerApi=new RegisterApi();

        handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch(msg.what){
                    case SEND_VERIGY:
                        LogUtil.Log("response===="+msg.obj+"");
                        parseJSONWithJSONObject(msg.obj.toString());
                }
            }
        };
    }

    private void parseJSONWithJSONObject(String jsonData){
        try {
            JSONObject jsonObject=new JSONObject(jsonData);
            ToastUtils.showToast(this,jsonObject.optString("message"));
            //LogUtil.Log(jsonObject.optString("message"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void getRegisterMsg(){
        phoneNum=edt_phoneNum.getText().toString().trim();
        password=edt_password.getText().toString().trim();
        verityNum=edt_verifyNum.getText().toString().trim();
    }

    private void setRegister(){

        registerApi.phone=phoneNum;
        registerApi.password=password;
        registerApi.verify=verityNum;
//        LogUtil.Log(registerApi.getUrl()+"?"+registerApi.getParamString());
        String result= HttpUtis.api(registerApi.getUrl(),registerApi.getParamString());
        parseJSONWithJSONObject(result);


    }

    @Override
    @OnClick({R.id.tv_right,R.id.tv_left,R.id.tv_getVerify,R.id.tv_register})
    public void onClick(View v) {
        super.onClick(v);
        getRegisterMsg();
        switch (v.getId()) {
            case R.id.tv_right:
                Intent loginintent=new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(loginintent);
                break;
            case R.id.tv_left:
                onBackPressed();
                break;
            case R.id.tv_getVerify:
                if(!TextUtils.isEmpty(phoneNum)){
                    time.start();
                    edt_verifyNum.setText("555555");
                }else{
                    ToastUtils.showToast(RegisterActivity.this,"请输入手机号和密码！");
                }
                break;
            case R.id.tv_register:
                getRegisterMsg();
                if(!TextUtils.isEmpty(phoneNum)&&!TextUtils.isEmpty(password)&&! TextUtils.isEmpty(verityNum)){
                    setRegister();
                }else{
                    ToastUtils.showToast(RegisterActivity.this,"请填写完成信息！");
                }
                break;
        }
    }

    class TimeCount extends CountDownTimer{

        public TimeCount(long millisInFuture, long countDownInterval){
            super(millisInFuture,countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            tv_getVerify.setClickable(false);
            tv_getVerify.setText("请稍等...("+millisUntilFinished/1000+")");

        }

        @Override
        public void onFinish() {
            tv_getVerify.setText("重新发送");
            tv_getVerify.setClickable(true);
        }
    }
}
